package xinyu.customer.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.logos.common.source.network.entity.BaseNetEntity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xinyu.customer.R;
import xinyu.customer.base.AppBaseActivity;
import xinyu.customer.cache.LoginCache;
import xinyu.customer.home.HomeActivity;
import xinyu.customer.login.data.LoginContract;
import xinyu.customer.login.data.LoginPresenter;
import xinyu.customer.login.data.LoginRemoteDataSource;
import xinyu.customer.bean.UserEntity;
import xinyu.customer.util.SpConstant;
import xinyu.customer.widget.CustomToastUtil;

/**
 * 登录界面
 */
public class LoginActivity extends AppBaseActivity implements LoginContract.View {

    @BindView(R.id.et_login_username)
    EditText etLoginUsername;
    @BindView(R.id.cb_login_pwd_visible)
    CheckBox cbLoginPwdVisible;
    @BindView(R.id.et_login_password)
    EditText etLoginPassword;
    @BindView(R.id.ll_input_layout)
    LinearLayout llInputLayout;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    private LoginContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        mPresenter = new LoginPresenter(this, new LoginRemoteDataSource());

    }

    private void initView() {
        cbLoginPwdVisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    etLoginPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etLoginPassword.setSelection(etLoginPassword.getText().toString().length());
                } else {
                    etLoginPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etLoginPassword.setSelection(etLoginPassword.getText().toString().length());
                }
            }
        });
    }

    @OnClick({R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                toLogin();
                break;
        }
    }

    private void toLogin() {
        String phone = etLoginUsername.getText().toString();
        String pswd = etLoginPassword.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            CustomToastUtil.showShort("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(pswd)) {
            CustomToastUtil.showShort("请输入密码");
            return;
        }

        mPresenter.getLoginList(phone, pswd);
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {

    }

    @Override
    public void onLoginSuccess(BaseNetEntity<UserEntity> userEntity) {
        if (userEntity.getCode()==1){
            SpConstant.saveUser(LoginActivity.this, userEntity.getResult());
            LoginCache.setAccount(userEntity.getResult().getCust_id());
            startActivity(new Intent(this, HomeActivity.class));
        }
    }
}
